var dialogstartheight="";
var filecount=0;
var uploadfilestr="<div style='margin-left:75px;text-align:left;' id='canremove'>"+"<div id='fileQueue' ></div>"+
"<input type='file' name='uploadify' id='uploadify' />"+"<label id='shangchuan'>上传</label>&nbsp;&nbsp;<label id='cancle'>取消</label>"+
"<div id='result'></div>"+"</div>";
var uploadfilecount=0;
var bind=0;
var scrollcount=10;
function getTop(height){
	var windowheight=window.innerHeight;
	return (windowheight-height)/2;
}
function getLeft(width){
	var windowwidth=window.innerWidth;
	return (windowwidth-width)/2;
}
function addToTup(){
	var uptotop=document.createElement("div");
}
function productionimgresign(){
	$(".productionimg").click(function(){//每个分享图片有一个产品id 根据此ID跳转到shareinfo页面
		var production_id=$(this).attr("alt");
		window.open("ShareInfo?_id="+production_id);
	});
}
function TotalCommentCountLink(){
	$(".pcomment_link").click(function(){//点击产品评论总数跳转到shareinfo页面
		var father=$(this).parent().parent().parent().parent().get(0);
		var _id=father.getElementsByTagName("img").item(0).alt;
		window.open("ShareInfo?_id="+_id);
	});
}
function UserNameLink(){
	$(".username1").click(function(){//根据第一条评论的用户名跳转到好友中心
		window.open("getFriendOwnMessageAction?uname="+$(this).html().trim());
	});
}
function FriendRank1Link(){
	$(".friendrank1").click(function(){//好友分享榜中点击用户名跳转到好友中心
		window.open("getFriendOwnMessageAction?uname="+$(this).get(0).id);//此处打开用户中心页面
	});
}
function FriendRank2Link(){
	$(".friendrank2").click(function(){//好友分享榜中点击用户名称跳转到好友中心
		window.open("getFriendOwnMessageAction?uname="+$(this).html().trim());
	});
}
function ProRank1Link(){
	$(".pro_rank1").click(function(){//人气分享榜中点击产品图片跳转到shareinfo页面
		window.open("ShareInfo?_id="+$(this).get(0).id);
	});
}
function ProRank2Link(){
	$(".pro_rank2").click(function(){//人气分享榜中点击分享名称跳转到shareinfo页面
		window.open("ShareInfo?_id="+$(this).get(0).id);
	});
}
function jubaoresign(){
	$(".share_jubao_right").click(function(){
		var receive_user="";
		receive_user=$(this).parent().parent().get(0).getElementsByTagName("a").item(0).innerHTML;
		if(receive_user=="")
			return;
		var commentId=$(this).parent().parent().get(0).getElementsByTagName("span").item(0).id;
		var left=getLeft(640);
		var top=getTop(300);
		$("#jubao").dialog({
			height:300,
			width:640,
			resizable:false,
			position:[left,top],
			buttons:{
				确定:function(){
					var jubao_content=$("#jubao_content").val();
					//alert(receive_user+jubao_content+commentId);
					if(jubao_content.length==0)
						alert("亲，您还没有输入举报的理由呢！");
					else{
						$.post('jubaoaction',{'jubao_content':jubao_content,'receive_user':receive_user,
							'commentId':commentId},function(data){
							if(data=="success")
							{
								alert("举报成功，我们会对该举报进行审核！");
								$("#jubao_content").val("");
								$("#jubao").dialog("close");
							}
							else{
								alert("举报失败!");
							}
						});
					}
				},
				取消:function(){
					$(this).dialog("close");
				}
			},
			modal:true,
			open:function(){
				$("#jubao_username").html("被举报人是:"+receive_user);
			}
		});
		$("#jubao").bind('dialogbeforeclose',function(){
            	$("#jubao_content").val("");
        });
	});
}
function siliaoresign(){
	$(".share_jubao_left").click(function(){
		var receiveuser="";	
		receiveuser=$(this).parent().parent().get(0).getElementsByTagName("a").item(0).innerHTML;
		if(receiveuser=="")
			return;
		var private_comment;
		$("#private_comm").append(uploadfilestr);
		$("#fileQueue").css("height","auto");
        var left=getLeft(670);
        var top=getTop(450);
        var filepath='';
		$("#private_comm").dialog({
			width:670,
			height:450,
			modal:true,
			resizable:false,
			position:[left,top],
			open:function(){
				dialogstartheight=$("#private_comm").dialog('option','height');
				document.getElementById("content").focus();
				$("#receive_userame").val(receiveuser);
				$("#content").val("");
				$("#filename").val("");
				$("#shangchuan").click(function(){
					$('#uploadify').uploadifyUpload();
				});
				$("#cancle").click(function(){
					$('#uploadify').uploadifyClearQueue();
				});
   			   	$("#uploadify").uploadify({
                	'uploader'       : 'uploadify/uploadify.swf', //是组件自带的flash，用于打开选取本地文件的按钮 
                	'script'         : 'uploadAction',//处理上传的路径，这里使用Struts2是XXX.action 
                	'cancelImg'      : 'uploadify/cancel.png',//取消上传文件的按钮图片，就是个叉叉
                	'folder'         : receiveuser+"1",//上传文件的目录
                	'fileDataName'   : 'uploadify',//和input的name属性值保持一致就好，Struts2就能处理了
                	'queueID'        : 'fileQueue',
                	'auto'           : false,//是否选取文件后自动上传
                	'multi'          : true,//是否支持多文件上传
                	'simUploadLimit' : 2,//每次最大上传文件数
                	'queueSizeLimit' : 5,
                	'buttonText'     : 'BROWSE FILE',//按钮上的文字
                	'displayData'    : 'speed',//有speed和percentage两种，一个显示速度，一个显示完成百分比 
                	'fileDesc'       : '支持格式:jpg/gif/jpeg/png/bmp.', //如果配置了以下的'fileExt'属性，那么这个属性是必须的 
                	'fileExt'        : '*.jpg;*.gif;*.jpeg;*.png;*.bmp;*.cdr',//允许的格式
                	'onComplete'     : function (event, queueID, fileObj, response, data){
                		filepath+=response+";";
                	},
                	'onSelect':function(){
                		filecount=filecount+1;
                		var height=$("#private_comm").dialog('option','height');
                		$("#private_comm").dialog('option','height',height+70);
                	},
                	'onCancle':function(event,queueId,fileObj,data){
                		filecount=data.fileCount;
                	},
                	'onClearQueue':function(){
                		filecount=0;
                		$("#private_comm").dialog('option','height',dialogstartheight);
                	}
            	});
				}, 
				buttons:{
					发送:function(){
						private_comment=document.getElementById("content").value;//内容
						if(private_comment.length==0)
						{
							alert("内容不能为空哦亲！");
						}
						else{
							var ques;
							if(filepath=='')
							{
								ques=window.confirm("亲，您暂时没有选择文件呦！");
							}
							$.post('siliaoaction',{'private_comment':private_comment,'receiveuser':receiveuser,'filepath':filepath},
							function(data){
								filepath="";
								if(data=="success")
								{
									left=getLeft(300);
									top=getTop(180);
									$("#error").html("发送成功！").dialog({
										autoOpen:true,
										width:300,
										height:180,
										position:[left,top],
										buttons:{
											确定:function(){
												$(this).dialog("close");
												$("#private_comm").dialog("close");
											}
										}
									});
									$("#content").val("");
								}
								else{
									alert("发送失败!");
								}
							});
						}
					},
					取消:function(){
						$("#canremove").remove();
						if(filepath!="")
						{
							$.post('deletefile',{"filepath":filepath},function(data){});
							filepath="";
						}
						$(this).dialog("close");
					}
				}
			});
			$("#private_comm").bind('dialogbeforeclose',function(){
				if(filepath!="")
				{
					$.post('deletefile',{"filepath":filepath},function(data){});
				}
            	$("#canremove").remove();
            	filepath="";
            });
	});
}
function btnUplodfileresign(){
	$(".btnUploadFile").click(function(){
    	var receive_production_id=$(this).parent().parent().parent().parent().get(0).getElementsByTagName("img").item(0).alt;
    	var filepath="";
    	$("#uploadfileorimg").dialog({
    		autoOpen:true,
    		width:600,
    		height:500,
    		modal:true,
    		resizable:false,
    		position:[getLeft(600),getTop(500)],
    		buttons:{
    			完成上传:function(){
    				$.post('sharecommentupload',{'filepath':filepath,'customId':receive_production_id},
    				function(data){//这里还差一个action
    					if(data=="success")
    					{
    						alert("上传成功");
        					filepath="";
            				$("#uploadfileorimg").dialog("close");
    					}
    				});
    			},
    			取消:function(){
    				if(filepath!="")
    				{
    					$.post('deletefile',{"filepath":filepath},function(data){});
    					filepath="";
    				}
    				$(this).dialog("close");
    			}
    		},
    		open:function(){
    			$(this).append(uploadfilestr).fadeIn();
    			$("#uploadify").uploadify({
                	'uploader'       : 'uploadify/uploadify.swf', 
                	'script'         : 'uploadAction', 
                	'cancelImg'      : 'uploadify/cancel.png',
                	'folder'         : receive_production_id+"0",
                	'fileDataName'   : 'uploadify',
                	'queueID'        : 'fileQueue',
                	'auto'           : false,
                	'multi'          : true,
                	'simUploadLimit' : 2,
                	'buttonText'     : 'BROWSE FILE',
                	'displayData'    : 'speed', 
                	'fileDesc'       : '支持格式:jpg/gif/jpeg/png/bmp.', 
                	'fileExt'        : '*.jpg;*.gif;*.jpeg;*.png;*.bmp;*.cdr',
                	'onComplete'     : function (event, queueID, fileObj, response, data){
                		filepath+=response+";";
           			}
          		});
          		$("#shangchuan").click(function(){
						$('#uploadify').uploadifyUpload();
					});
					$("#cancle").click(function(){
						$('#uploadify').uploadifyClearQueue();
					});
          		$("#close_uploadfile").click(function(){
    				$("#canremove").remove();
    				$(this).parent().parent().fadeOut();
    			});
    		}
    	});
    	$("#uploadfileorimg").bind('dialogbeforeclose',function(){
    			if(filepath!="")
    			{
    				$.post('deletefile',{"filepath":filepath},function(data){});
    				filepath="";
    			}
            	$("#canremove").remove();
        });
    });
}
function onScroll(){
	var ifload=($(document).scrollTop())>=($(document).height()-window.innerHeight);
	if(ifload>0)
	{
		if(bind==1)
		{
			$.post("loadsharechild",{"startcount":scrollcount,"endcount":scrollcount+10},function(data){
				scrollcount=scrollcount+10;
				if(parseInt(data.total)>0)
				{
					var username1=eval("("+data.username1json+")");
					var imgpath=eval("("+data.imgpathjson+")");
					var username1_comment=eval("("+data.username1_commentjson+")");
					var username2=eval("("+data.username2json+")");
					var username2_comment=eval("("+data.username2_commentjson+")");
					var comment_count=eval("("+data.comment_countjson+")");
					var production_id=eval("("+data.production_id+")");
					var fc=eval("("+data.firstcomment+")");
					var sc=eval("("+data.secondcomment+")");
					for(var i=0;i<parseInt(data.total);i++)
					{
						$("#sharechild_container").append("<div class='share_content_left_child'>"+
						"<div class='share_content_left_child_img'>"+"<img src='"+imgpath.imgpath[i]+"' alt='"+production_id.id[i]+
						"' class='productionimg'/>"+"</div>"+
            			"<div class='share_content_left_child_text'>"+"<div class='share_content_left_child_usertext'>"+
            			"<div class='share_usertext_child'>"+"<div class='share_usertext_child_comment'>"+
            			"[<a class='username1'>"+username1.username1[i]+"</a>]&nbsp;"+"<span id="+fc.fc[i]+">"
            			+username1_comment.user1_comment[i]+"</span>"+"</div>"+
            			"<div class='share_jubao'>"+"<div class='share_jubao_left'>私聊</div>"+
            			"<div class='share_jubao_right'>举报它</div>"+"</div>"+"</div>"+"<div class='share_usertext_child'>"+
            			"<div class='share_usertext_child_comment'>"+
            			"[<a class='username1'>"+username2.username2[i]+"</a>]&nbsp;"+"<span id="+sc.sc[i]+">"
            			+username2_comment.user2_comment[i]+"</span>"+"</div>"+
            			"<div class='share_jubao'>"+"<div class='share_jubao_left'>私聊</div>"+
            			"<div class='share_jubao_right'>举报它</div>"+"</div>"+"</div>"+"</div>"+
            			"<div class='share_content_left_child_comment'>"+
            			"<div class='share_child_showcomment'>"+"显示更多评论(<a class='pcomment_link'>"+comment_count.commentcount[i]+"</a>)"+"</div>"+
            			"<div class='share_child_commenttext'>"+"<input type='text' value=''/>"+"</div>"+
						"<div class='share_child_commentbtn'>"+
            			"<input type='button' value=' 上传图片或文件' class='btnUploadFile'/>"+
            			"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+
            			"<input type='button' value='发表评论' class='btnPublish' style='color:rgb(122,3,3)'/>"+
            			"</div>"+"</div>"+"</div>"+"</div>");
					}
					$(".btnUploadFile").unbind("click");
					$(".share_jubao_right").unbind("click");
					$(".btnPublish").unbind("click");
					$(".share_jubao_left").unbind("click");
					$(".productionimg").unbind("click");
					$(".pcomment_link").unbind("click");
					$(".usernam1").unbind("click");
					btnPublishresign();
    				btnUplodfileresign();
					siliaoresign();
					jubaoresign();
					productionimgresign();
					TotalCommentCountLink();
				}
				else{
					alert("已经没有数据了亲!");
				}
			},"json");
		}
	}
}
function btnPublishresign(){//此处处理评论操作
	$(".btnPublish").click(function(){
		var temp=$(this).parent().parent().parent();
		var domelement=$(this).parent().parent().get(0).getElementsByTagName("input").item(0);
		var father=$(this).parent().parent().get(0).getElementsByTagName("a").item(0);
    	var comment_content=domelement.value;
    	var production_id=$(this).parent().parent().parent().parent().get(0).getElementsByTagName("img").item(0).alt;
    	if(comment_content.length==0)
    	{
    		alert("亲，您还没有内容哦！");
    	}
    	else{
    		$.post("user_comment",{"comment_content":comment_content,"production_id":production_id},function(data){
    			if(data="success")
    			{
    				alert("评论成功!");
    				var count=parseInt(father.innerHTML);
    				//InsertComment(count,temp,father,comment_content,"test_needchange");
    				domelement.value="";
    			}
    			else{
    				alert("评论失败!");
    			}
    		});
    	}
    });
}
function InsertComment(count, temp,father,comment_content,username){
	if(count<2)
	{
		temp.get(0).getElementsByTagName("a").item(count).innerHTML=username;
		temp.get(0).getElementsByTagName("span").item(count).innerHTML=comment_content;
	}
	father.innerHTML=count+1;
}
function jPagesresign(id,classname){
		$(""+classname).jPages({
			containerID: id,
            first: '首页',
            last: '尾页',
            previous: '上页',
            next: '下页',
            perPage: 5,
            startPage: 1,
            startRange: 2,
            midRange: 3,
            endRange: 2,
            animation: '',//有以下几种选择 flash bounce shake tada swing wobble wiggle pulse
            keyBrowse: true,
            callback: function (pages, items) {
                 items.showing.find("img").trigger("turnPage");
                 items.oncoming.find("img").trigger("turnPage");
            }
		});
}
function sharefriendrank(){
	$.post("loadfriendrank",{},function(data){
		var userface=eval("("+data.userface+")");//已经将信息传递过来了
		var username=eval("("+data.username+")");
		var userhot=eval("("+data.userhot+")");
		var str="";
		for(var i=0;i<parseInt(data.total);i++)
		{
			str+="<div class='share_content_right_child' >" +
                "<div class='share_right_child_img' >" +
                    "<span class='friendrank1' id='"+username.username[i]+"'><img src='"+userface.userface[i]+"' ></span>"+
                "</div>" +
                "<div class='share_right_child_userinfo'>" +
                    "<div class='share_right_child_userinfo_name' >" +
                        "<div style='float:left;'><span class='friendrank2'>"+username.username[i]+"</span></div>" +
                    "</div>" +
                    "<div class='share_right_child_userinfo_message' >" +
                    "<div style='float:left;'>指数：<span>"+(userhot.userhot[i])+"</span></div>" +
                    "</div>" +
                "</div>" +
          "</div>";
		}
		$("#itemContainer1").append(str);
		jPagesresign("itemContainer1",".holder1");
		$("div").css("border-radius",15+"px");
		FriendRank1Link();
		FriendRank2Link();
	});
}
function sharerenqirank(){
	$.post("loadrenqirank",{},function(data){
		var pimg=eval("("+data.production_img+")");
		var pname=eval("("+data.production_name+")");
		var phot=eval("("+data.production_hot+")");
		var pid=eval("("+data.pro_id+")");
		var str="";
		for(var i=0;i<parseInt(data.total);i++)
		{
			str+="<div class='share_content_right_child' >" +
                "<div class='share_right_child_img' >" +
                    "<span class='pro_rank1' id='"+pid.P_id[i]+"'><img src='"+pimg.Pface[i]+"' ></span>" +
                "</div>" +
                "<div class='share_right_child_userinfo'>" +
                    "<div class='share_right_child_userinfo_name' >" +
                        "<div style='float:left;'><span class='pro_rank2' id='"+pid.P_id[i]+"'>"+pname.Pname[i]+"</span></div>" +
                    "</div>" +
                    "<div class='share_right_child_userinfo_message' >" +
                    "<div style='float:left;'>指数：<span>"+(phot.Phot[i])+"</span></div>" +
                    "</div>" +
                "</div>" +
          "</div>";
		}
		$("#itemContainer").append(str);
		jPagesresign("itemContainer",".holder");
		$("div").css("border-radius",15+"px");
		$("a").attr("target","_blank");
		ProRank1Link();	
		ProRank2Link();
	});
}

$(document).ready(function(e) {
	function showResult(){//删除显示的上传成功结果
          $("#result").html("");
    }
    function uploadFile(){//上传文件
         jQuery('#uploadify').uploadifyUpload();
    }
    function clearFile(){//清空所有上传队列
         jQuery('#uploadify').uploadifyClearQueue();
    }
    sharefriendrank();
    sharerenqirank();
    btnPublishresign();
    btnUplodfileresign();
	siliaoresign();
	jubaoresign();
	productionimgresign();
	/*var top1=document.createElement("div");
	top1.id="top1";
	top1.style.cssText="width:71px; height:65px; border-radius:10px; -webkit-border-radius:10px;"+
			"-moz-border-radius:5px;bottom:50px; right:50px; position:fixed;background-color:#CCC;"+
			"cursor:pointer;color:Black; font-size:30px; padding-left:5px;padding-top:10px;" +
			"font-family:'Arial Black', Gadget, sans-serif; font-weight:bolder;";
	top1.innerHTML="<strong>Top</strong>";
	document.body.appendChild(top1);*/
	/*$("#top1").click(function(){
		//$(document).scrollTop(0);
		window.scrollTo(0);
	});*/
	bind=1;
	TotalCommentCountLink();
	UserNameLink();
	window.onscroll=onScroll;
	$("a").css('cursor','pointer');
});